# _*_ coding: utf-8 _*_
"""
@ 时间    ：2024/10/27 18:34
@ 作者    ：旺财
@ 文件    ：01 Apriori算法实现.py
@ 说明    ：通过apyori库实现apriori算法
"""
from apyori import apriori

goods = [['A', 'B', 'C'], ['A', 'B'], ['B', 'C'], ['A', 'B', 'C', 'D'], ['B', 'C', 'D']]

rules = apriori(goods, min_support=0.4, min_confidence=0.8)  # min_support:最小支持度, min_confidence:最小置信度
results = list(rules)
print(results)

for i in results:
    for j in i.ordered_statistics:
        X = j.items_base    # 关联规则前件
        Y = j.items_add     # 关联规则后件
        x = ', '.join([item for item in X])
        y = ', '.join([item for item in Y])
        if x:
            print(f'{x} -> {y}')